package com.ogawa.chair7808.ui.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.provider.Settings;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.ogawa.chair7808.R;
import com.ogawa.chair7808.observer.SoftApSucceedObserver;
import com.ogawa.chair7808.ui.iview.ISoftApNewView;
import com.ogawa.chair7808.ui.iview.ISoftApOldView;
import com.ogawa.chair7808.ui.presenter.SoftApNewPresenter;
import com.ogawa.chair7808.view.SoftApDialog;
import com.example.reslib.view.SoftApPopwindow;
import com.example.reslib.view.SoftApPopwindowAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: bright.lin@medisana.net.cn
 * 时间: 2019/1/2
 * 标题: 配网Activity（8.0以上版本）
 * 描述: 配网功能View实现
 * 修改: 由 bright.lin@medisana.net.cn 修改于 2019/1/2
 */
public class SoftApNewActivity extends BaseActivity implements ISoftApNewView, View.OnClickListener, PopupWindow.OnDismissListener, SoftApPopwindowAdapter.OnItemClickListener, SoftApSucceedObserver.onSoftApLis, SoftApDialog.OnConfirmListener {
    private static final String TAG = SoftApNewActivity.class.getName();

    // 跳转WIFI设置页面的请求码
    private static final int OPEN_WIFI_SETTING = 1000;

    // Presenter对象
    private SoftApNewPresenter softApPresenter;
    // 自定义Popwindow-用于显示wifi下拉列表
    private SoftApPopwindow softApPopwindow;
    // 自定义Dialog
    private SoftApDialog softApDialog;
    // 加载中Dialog
    private Dialog loadingDialog;

    // TextView-SSID选择框
    private TextView selectSsidTv;
    // TextView-SSID选择框下拉图案
    private TextView selectSsidImg;
    // EditText-密码输入框
    private EditText inputPswEt;

    // 当前显示的Ssid值
    private String mSsid;

    // 是否跳转设置
    private boolean goSetting = false;

    // 是否已释放资源标志位
    private boolean isDestroyed = false;

    /**
     * 初始化标题
     */
    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setGreenBackBotton();
        titleBar.showConnectWifi();
        setBackGround(R.mipmap.bg);
    }

    /**
     * @see BaseActivity#initView(int)
     */
    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
        softApPresenter.init();
        SoftApSucceedObserver.getInst().registerObserver(this);
    }

    /**
     * @see Activity#onPause()
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            destroy();
        }
    }

    /**
     * @see Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
        SoftApSucceedObserver.getInst().removeObserver(this);
    }

    /**
     * @see FragmentActivity#onActivityResult(int, int, Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_WIFI_SETTING) {
            softApPresenter.afterSetting();
        }
    }

    /**
     * 设置布局
     *
     * @return 布局id
     */
    @Override
    public int setLayoutId() {
        return R.layout.activity_softap;
    }

    /**
     * 点击事件
     *
     * @param v View对象
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.softap_tv_select_img || id == R.id.softap_tv_select_ssid) {
            selectSsidImg.setVisibility(View.INVISIBLE);
            selectSsidTv.setHint("");
            selectSsidTv.setText("");
            softApPopwindow.setWidth(selectSsidTv.getWidth());
            softApPopwindow.showPopupWindow(selectSsidTv);
        } else if (id == R.id.softap_btn_confirm) {
            softApPresenter.confirm(inputPswEt.getText().toString());
        } else if (id == R.id.softap_btn_refresh) {
            softApPresenter.scan();
        }
    }

    /**
     * popwindow显示的ssid列表点击事件
     *
     * @param scanResult wifi搜索结果
     */
    @Override
    public void onItemClick(ScanResult scanResult) {
        softApPresenter.setScanResult(scanResult);
        mSsid = scanResult.SSID;
        softApPopwindow.dismiss();
    }

    /**
     * 取消显示popwindow
     */
    @Override
    public void onDismiss() {
        selectSsidImg.setVisibility(View.VISIBLE);
        if (null != mSsid) {
            selectSsidTv.setText(mSsid);
        } else {
            selectSsidTv.setHint(R.string.softap_hint_select_ssid);
        }
    }

    /*ISoftApView接口*/

    /**
     * @see ISoftApOldView#onWifiList(List)
     */
    @Override
    public void onWifiList(final List<ScanResult> data) {
        softApPopwindow.update(data);
    }

    @Override
    public void setSettingFlag() {
        goSetting = true;
    }

    /**
     * @see ISoftApOldView#showDialog(int, int)
     */
    @Override
    public void showDialog(final int tipOne, final int tipTwo) {
        if (softApDialog != null && softApDialog.isShowing()) {
            softApDialog.dismiss();
        }
        softApDialog = new SoftApDialog(SoftApNewActivity.this, SoftApDialog.TWO_BTN_TYPE);
        softApDialog.setOnConfirmListener(this);
        softApDialog.setOnDismissListener(dialog -> softApPresenter.checkSuccess());
        softApDialog.show();
        softApDialog.setTipOne(getString(tipOne));
        softApDialog.setTipTwo(getString(tipTwo));
    }

    /**
     * @see ISoftApOldView#showWaiting()
     */
    @Override
    public void showWaiting() {
        loadingDialog = new Dialog(this, R.style.SoftApDialog);
        loadingDialog.setContentView(R.layout.layout_softap_progressbar);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

    /**
     * @see ISoftApOldView#dismissWaiting()
     */
    @Override
    public void dismissWaiting() {
        if (null != loadingDialog && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    /**
     * @see ISoftApOldView#onFinish()
     */
    @Override
    public void onFinish() {
        this.finish();
    }
    /*ISoftApView接口*/

    /**
     * 配网完成
     */
    @Override
    public void isSoftApOk() {
        this.finish();
    }

    @Override
    public void onConfirm() {
        dismissDialog();
        if (goSetting) {
            goSetting = false;
            startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), OPEN_WIFI_SETTING);
        }
    }

    /**
     * 初始化界面
     */
    private void initView() {
        softApPresenter = new SoftApNewPresenter(this, this);
        TextView tipOneTv = findViewById(R.id.softap_tv_tip_one);
        TextView tipTwoTv = findViewById(R.id.softap_tv_tip_two);
        tipOneTv.setText(R.string.softap_tip1_connect_wifi);
        tipTwoTv.setText(R.string.softap_tip2_connect_wifi);
        selectSsidTv = findViewById(R.id.softap_tv_select_ssid);
        selectSsidImg = findViewById(R.id.softap_tv_select_img);
        inputPswEt = findViewById(R.id.softap_tv_input_pwd);
        ImageView refreshBtn = findViewById(R.id.softap_btn_refresh);
        TextView confirmBtn = findViewById(R.id.softap_btn_confirm);
        selectSsidTv.setOnClickListener(this);
        selectSsidImg.setOnClickListener(this);
        refreshBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        softApPopwindow = new SoftApPopwindow(this, new ArrayList<>(), this);
        softApPopwindow.setOnDismissListener(this);
    }

    private void dismissDialog() {
        if (null != softApDialog && softApDialog.isShowing()) {
            softApDialog.dismiss();
        }
    }

    /**
     * 释放资源
     */
    private void destroy() {
        if (isDestroyed) {
            return;
        }
        dismissWaiting();
        loadingDialog = null;
        if (null != softApDialog) {
            softApDialog.dismiss();
            softApDialog = null;
        }
        if (null != softApPopwindow) {
            softApPopwindow.dimissPopupWindow();
            softApPopwindow = null;
        }
        softApPresenter.onDestroy();
        isDestroyed = true;
    }
}
